
package com.feib.soeasy.dao.hibernate;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.ColumnValidateErrorDao;
import com.feib.soeasy.model.ColumnValidateError;
import com.feib.soeasy.pagination.PaginatedList;


/**
 * @title (#)ColumnValidateErrorHibernateDao.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/14 create by Jimmy Liu<br>
 */
@Repository("columnValidateErrorDao")
public class ColumnValidateErrorHibernateDao extends BaseHibernateDao<ColumnValidateError, Long> implements ColumnValidateErrorDao {

    @SuppressWarnings("unchecked")
    public PaginatedList<ColumnValidateError> queryPaginationByColumnValidateError(ColumnValidateError columnValidateError, PaginatedList<ColumnValidateError> paginationConfig) {

            PaginatedList<ColumnValidateError> result = new PaginatedList<ColumnValidateError>();
            result.setPageSize(paginationConfig.getPageSize());
            result.setPageNumber(paginationConfig.getPageNumber());
            result.setSortCriterion(paginationConfig.getSortCriterion());
            result.setSortDirection(paginationConfig.getSortDirection());

            result.setFullListSize(super.countByDetachedCriteria(createDetachedCriteriaByColumnValidateError(columnValidateError)));
            
            // 筆數大於0
            if (0 < result.getFullListSize())
            {
                DetachedCriteria criteria = createDetachedCriteriaByColumnValidateError(columnValidateError);

                //排序
                if (null != paginationConfig.getSortCriterion())
                {
                    // 由小到大
                    if (PaginatedList.SORT_DIRECTION_ASC == paginationConfig.getSortDirection())
                    {
                        criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                    }
                    else // 由大到小
                    {
                        criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                    }
                }


                int maxResults = result.getPageSize(); //the maximum number of results
                int firstResult = result.getFirstIndexForCurrentPage();  //the first result to retrieve, numbered from 0
                result.setPaginatedList(this.getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults));
            }

            return result;
        }
    
    protected DetachedCriteria createDetachedCriteriaByColumnValidateError(ColumnValidateError entity)
    {
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);

        Example example = Example.create(entity);
        example.enableLike(MatchMode.START);
        example.excludeZeroes();
        criteria.add(example);

        // 屬於哪一個群組
        if (null != entity.getUploadFile())
        {
            DetachedCriteria detachedCriteria = criteria.createCriteria("uploadFile");

            // PK 有值，需加入為查詢條件
            if (null != entity.getUploadFile().getUploadFilePk())
            {
                detachedCriteria.add(Restrictions.idEq(entity.getUploadFile().getUploadFilePk()));
            }

            // UploadFile 其他查詢條件
            Example e = Example.create(entity.getUploadFile());
            e.enableLike(MatchMode.START);
            e.excludeZeroes();
            detachedCriteria.add(e);
        }
        return criteria;
    }
}
